var app = getApp();
var api = require('../../utils/util.js').default;
Page({
  data: {
    show_permission: "未授权",
    permission: false,
    time: false,
    sub_time: null,
    place: false,
    sub_place: null,

    order: false,
    order_num: null,
    show_order: "填写单次派送最大订单数量",
    order_max: ['1 单', '2 单', '3 单', '4 单', '5 单'],

    details: false,
    sub_details: null,

  },

  // 获取授权
  getPermission: function () {
    wx.showModal({
      title: '确认申请',
      content: 'upstairs平台在您申请成为骑手后，会将您的头像、昵称等必要信息提供给下单的用户。是否允许授权？',
      showCancel: true,
      cancelText: '允许',
      cancelColor: '#3CC51F',
      confirmText: '取消',
      confirmColor: '#000000',
      success: (result) => {
        if (result.confirm) { //拒绝
          this.setData({
            show_permission: "未授权",
            permission: false
          })
        } else {
          this.setData({
            show_permission: "已授权",
            permission: true
          })
        }
      },
    });
  },

  // 获取接单时间
  getTime: function (e) {
    var time = e.detail.value;
    if (time.length > 1) {
      this.setData({
        time: true,
        sub_time: time
      })
    } else {
      this.setData({
        time: false
      })
    }
  },

  // 获取派送区域
  getPlace: function (e) {
    var place = e.detail.value;
    if (place.length > 1) {
      this.setData({
        place: true,
        sub_place: place
      })
    } else {
      this.setData({
        place: false
      })
    }
  },

  // 获取最大单数
  getMaxOrder: function (e) {
    if (e.detail.value) {
      var order_num = parseInt(e.detail.value) + 1
      var show_order = this.data.order_max[order_num - 1];
      this.setData({
        order: true,
        order_num: order_num,
        show_order: show_order
      })
    } else {
      this.setData({
        order: false,
        show_order: "填写单次派送最大订单数量",
      })
    }
  },

  // 获取备注
  getDetails: function (e) {
    var details = e.detail.value;
    if (details.length > 1) {
      this.setData({
        details: true,
        sub_details: details
      })
    } else {
      this.setData({
        details: false
      })
    }
  },

  // 提交申请
  submitApply: function () {
    if (this.data.permission && this.data.time && this.data.place && this.data.order && this.data.details) {
      wx.showModal({
        title: '温馨提示',
        content: '请确认信息：\n\n授权状态：已授权\n接单时间：' + this.data.sub_time + '\n派送区域：' + this.data.sub_place + '\n单次派送数：' + this.data.show_order + '\n备注细节：' + this.data.sub_details,
        showCancel: true,
        cancelText: '确认',
        cancelColor: '#3CC51F',
        confirmText: '取消',
        confirmColor: '#000000',
        success: (result) => {
          if (result.confirm) {
            console.log("取消")
          } else {
            var reg_time = api.formatTime(new Date());
            var data = {
                openid: app.globalData.openid,
                user_name: app.globalData.user_name,
                school_num: app.globalData.school_num,
                user_pho: app.globalData.user_pho,
                user_img: app.globalData.user_img,
                reg_time: reg_time,
                time: this.data.sub_time,
                place: this.data.sub_place,
                ordernum: this.data.order_num,
                details: this.data.sub_details,
            }
            console.log(data)
            api.applyRider(data).then(res => {
              console.log(res.data);
              if(res.data.set_rider&&res.data.set_user){
                wx.showToast({
                  title: '提交成功',
                  icon: 'success',
                  duration: 1000,
                });
                app.globalData.is_rider='2';
              }else{
                wx.showToast({
                  title: res.data.errmsg,
                  icon: 'loading',
                  duration: 1500,
                });     
              }
            })
          }
        },
      });
    } else {
      wx.showModal({
        title: '温馨提示',
        content: '申请表部分内容过短或部分未填写',
        showCancel: false,
        confirmText: '确定',
        confirmColor: '#3CC51F',
      });
    }
  }
})